#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/CI/lib -I/data/Software/mydan/CI/private/lib
use strict;
use warnings;
use JSON;
use Logs;
use FindBin qw( $RealBin );
use File::Basename;
use LWP::UserAgent;
use Encode;
use Util;

$| ++;

=head1 SYNOPSIS

    db => $mysql,
    id => id,

    logs => 日志对象

    addr => ''
    ticketid => ''
    treeid => ''

=cut

return sub
{
    my %param = @_;

    my ( $db, $id, $logs, $addr, $ticketid, $treeid )
        = @param{qw( db id logs addr ticketid treeid )};

    $logs = Logs->new( 'code.findtags.openc3fileserver' ) unless $logs;
    $logs->die( "id format error" ) unless $id =~ /^\d+$/;

    my $ua = LWP::UserAgent->new();
    $ua->agent('Mozilla/9 [en] (Centos; Linux)');
    $ua->default_header( Util::envinfo( qw( appkey appname ) ) );
    $ua->timeout( 10 );
    my $res = $ua->get( "http://api.job.open-c3.org/fileserver/$treeid" );
    my $cont = $res->content;
    $logs->die( "calljob fail: $cont" ) unless $res->is_success;

    my $data = eval{JSON::from_json $cont};
    $logs->die( "calljob fail: $cont : no json $@" ) if $@;

    $logs->die( "calljob fail: $cont : stat fail" ) unless $data->{stat};
    return map{ $_->{name} }@{$data->{data}}
}
